package com.easy.easybanner.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.icu.util.Measure;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.easy.easybanner.R;


/**
 * Created by Sigthp-P-Department on 2018/4/25.
 */

public class EasyBannerView extends RelativeLayout{

    private TextView mTvTitle;
    private EasyBannerViewPager mEasyBannerViewPager;
    private EasyPagerAdapter mEasyPagerAdapter;
    private Context mContext;
    private LinearLayout mLlIndicator;
    private int mCurrentPosition=0;
    private int mDedaultColor=Color.WHITE;
    private int mSelectorColor=Color.RED;
    private int mDefaultSize=8;
    private int mIndicatorMarginLeft=5;
    private int mDefaultIntervalTime=3000;
    private int mDefaultAnimationTime=850;
    private int mBackageColor=getResources().getColor(R.color.describleBackageColor);
    private RelativeLayout mRlDescrible;
    private int mTitleTextColor=Color.WHITE;
    private int mHeightRatio;
    private int mWrightRatio;

    public EasyBannerView(Context context) {
        this(context,null,0);
    }

    public EasyBannerView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public EasyBannerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext=context.getApplicationContext();
        inflate(context, R.layout.easy_banner_view,this);
        initview();
        initattr(attrs);

    }

    private void initattr(AttributeSet attrs) {
        TypedArray array = mContext.obtainStyledAttributes(attrs, R.styleable.EasyBannerView);

        mDedaultColor=array.getColor(R.styleable.EasyBannerView_indicatorDefaultColor,mDedaultColor);

        mSelectorColor=array.getColor(R.styleable.EasyBannerView_indicatorSelectorColor,mSelectorColor);

        mDefaultSize= (int) array.getDimension(R.styleable.EasyBannerView_indicatorSize,mDefaultSize);

        mIndicatorMarginLeft= (int) array.getDimension(R.styleable.EasyBannerView_indicatorMarginLeft,mIndicatorMarginLeft);

        // 滚动间隔
        mDefaultIntervalTime=array.getInt(R.styleable.EasyBannerView_rollingIntervalTime, mDefaultIntervalTime);
        mEasyBannerViewPager.setDuration(mDefaultIntervalTime);
        //滚动动画时间
        mDefaultAnimationTime=array.getInt(R.styleable.EasyBannerView_rollAnimationTime, mDefaultAnimationTime);
        mEasyBannerViewPager.setScrollerDuration(mDefaultAnimationTime);

        mBackageColor=array.getColor(R.styleable.EasyBannerView_describeBackageColor,mBackageColor);
        mRlDescrible.setBackgroundColor(mBackageColor);

        mTitleTextColor=array.getColor(R.styleable.EasyBannerView_titleTextColor,mTitleTextColor);
        mTvTitle.setTextColor(mTitleTextColor);

        //宽高比
        mHeightRatio=array.getInt(R.styleable.EasyBannerView_heightRatio,mHeightRatio);
        mWrightRatio=array.getInt(R.styleable.EasyBannerView_wrightRatio,mWrightRatio);
        array.recycle();
    }


    private void initview() {
        mTvTitle = findViewById(R.id.tv_title);
        mLlIndicator = findViewById(R.id.ll_indicator);
        mEasyBannerViewPager = findViewById(R.id.easy_banner_viewpager);
        mRlDescrible = findViewById(R.id.rl_describle);
    }

    public void setEasyPagerAdapter(EasyPagerAdapter easyPagerAdapter) {
        this.mEasyPagerAdapter=easyPagerAdapter;
        mEasyPagerAdapter.setTitle(mTvTitle,mCurrentPosition);
        mEasyBannerViewPager.setEasyPagerAdapter(easyPagerAdapter);
        initIndicator();
        mEasyBannerViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                pagerSelected(position);
            }
        });


    }

    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);
        if(mHeightRatio==0 || mWrightRatio==0){
            return;
        }
        int width = getMeasuredWidth();
        int height=(width*mHeightRatio/mWrightRatio);
        getLayoutParams().height=height;
    }

    private void pagerSelected(int position) {
        IndicatorView oldIndicatorView = (IndicatorView) mLlIndicator.getChildAt(mCurrentPosition);
        oldIndicatorView.setDrawable(mDedaultColor);

        mCurrentPosition = position % mEasyPagerAdapter.getIndicatorSize();
        IndicatorView indicatorView = (IndicatorView) mLlIndicator.getChildAt(mCurrentPosition);
        indicatorView.setDrawable(mSelectorColor);

        mEasyPagerAdapter.setTitle(mTvTitle,mCurrentPosition);
    }

    private void initIndicator() {
        int indicatorSize = mEasyPagerAdapter.getIndicatorSize();
        for(int x=0;x<indicatorSize;x++){
            IndicatorView indicatorView = new IndicatorView(mContext);
            LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(mDefaultSize,mDefaultSize);
            layoutParams.setMargins(mIndicatorMarginLeft,0,0,0);
            indicatorView.setLayoutParams(layoutParams);
            if(x==0){
                indicatorView.setDrawable(mSelectorColor);
            }else{
                indicatorView.setDrawable(mDedaultColor);
            }
            mLlIndicator.setGravity(Gravity.RIGHT);
            mLlIndicator.addView(indicatorView);
        }
    }

    private int dip2px(int i) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,i,getResources().getDisplayMetrics());
    }



    public EasyBannerView setAutomaticRolling() {
        mEasyBannerViewPager.setAutomaticRolling();
        return this;
    }

}
